/*
Copyright 2010 Bryce Alcock

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.com

 */

/*
 * NewDestinationDialog.java
 *
 * Created on Nov 12, 2010, 10:38:42 PM
 */

package com.ecocrypt.proxy.forwarder.gui;

import com.ecocrypt.proxy.forwarder.remote.CommandConsole;
import com.ecocrypt.proxy.forwarder.remote.ForwarderDestination;
import java.io.IOException;
import javax.swing.JFrame;

/**
 *
 * @author bryce
 */
public class NewDestinationDialog extends javax.swing.JDialog {

    RemoteForwarderView forwarderView ;
    /** Creates new form NewDestinationDialog */
    public NewDestinationDialog(RemoteForwarderView rfv) {
        super(new JFrame(), true);
        initComponents();
	forwarderView = rfv;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
        private void initComponents() {

                addButton = new javax.swing.JButton();
                cancelButton = new javax.swing.JButton();
                jLabel1 = new javax.swing.JLabel();
                jLabel2 = new javax.swing.JLabel();
                hostTextField = new javax.swing.JTextField();
                portTextField = new javax.swing.JTextField();

                setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
                getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

                addButton.setText("Add");
                addButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                addButtonActionPerformed(evt);
                        }
                });
                getContentPane().add(addButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 130, -1, -1));

                cancelButton.setMnemonic('c');
                cancelButton.setText("Cancel");
                cancelButton.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                                cancelButtonActionPerformed(evt);
                        }
                });
                getContentPane().add(cancelButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 130, -1, -1));

                jLabel1.setText("Destination Machine:");
                getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 40, -1, -1));

                jLabel2.setText("Destination Service Port:");
                getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 80, -1, -1));

                hostTextField.setText("IP/Host");
                getContentPane().add(hostTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 40, 80, -1));

                portTextField.setText("port");
                getContentPane().add(portTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 80, 80, -1));

                pack();
        }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
	//  Dismiss.
	this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
	//  add.

	// Needs some Error Handling:
	String host = hostTextField.getText();
	int port = Integer.parseInt(portTextField.getText());
	ForwarderDestination fd = new ForwarderDestination();
	fd.setIpAddress(host);
	fd.setPort(port);
	try{
	    CommandConsole.createForwardDestination(fd, null);
	}catch(IOException e){
	    e.printStackTrace();
	}
	this.dispose();
    }//GEN-LAST:event_addButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                NewDestinationDialog dialog = new NewDestinationDialog(null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton addButton;
        private javax.swing.JButton cancelButton;
        private javax.swing.JTextField hostTextField;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JTextField portTextField;
        // End of variables declaration//GEN-END:variables

}
